<?php

namespace CMS\Controller;

use Home\Base\Controller;

use CMS\Model\File;
use CMS\Model\Content\Download;

/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */
class FileController extends Controller
{

    /* 文件上传 */
    public function upload()
    {
        $return = array(
            'status' => 1,
            'info' => '上传成功',
            'data' => ''
        );
        /* 调用文件上传组件上传文件 */
        $File = new File;
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info = $File->upload($_FILES, C('DOWNLOAD_UPLOAD'), C('DOWNLOAD_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));
        
        /* 记录附件信息 */
        if ($info) {
            $return['data'] = think_encrypt(json_encode($info['download']));
        } else {
            $return['status'] = 0;
            $return['info'] = $File->getError();
        }
        
        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /* 下载文件 */
    public function download($id = null)
    {
        if (empty($id) || ! is_numeric($id)) {
            $this->error('参数错误！');
        }
        
        $logic = new Download;
        if (! $logic->download($id)) {
            $this->error($logic->getError());
        }
    }
}
